home *** CD-ROM | disk | FTP | other *** search
Wrap
/* File: :JonsCommands:Snag Object Demo:main.c Contains: Example code for using resources created by "snag object" osax. Written by: Wayne Walrath & Jon Pugh Copyright: © 1996 by Jon Pugh <jonpugh@netcom.com>, all rights reserved. Free use is granted, although I would like my name in your about box. Change History (most recent first): 6/23/96 Jon Pugh Expanded examples 6/3/96 Wayne Walrath Initial example */ #include <string.h> #include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <SIOUX.h> #define kSnagObjType 'obj ' static char *AskLine( const char *msg, char *s) { printf("%s", msg); gets( s ); printf("\n"); return( s ); } static void PrintObjList( void ) { OSErr err = noErr; short count, rID, j; Handle rHandle; ResType rType; Str255 rName; count = Count1Resources( kSnagObjType ); if( count ) { printf("Snag objects in this file: \n"); SetResLoad( FALSE ); /* do not need resource, just info */ for( j=1; j <= count; j++ ) { rHandle = GetIndResource(kSnagObjType, j ); GetResInfo( rHandle, &rID, &rType, rName ); printf("\t%6d, %s\n", rID, PtoCstr(rName)); } SetResLoad(TRUE); /* better do this! */ }else printf("No snag objects found in application's resource fork.\n"); } static void SendFinderEvent( DescType eventClass, DescType eventID, Handle objHandle ) { OSErr err = noErr; AppleEvent event, reply; AEDesc targetAddress; OSType finderSig = 'MACS'; event.dataHandle = reply.dataHandle = targetAddress.dataHandle = nil; if(! (err = AECreateDesc( typeApplSignature, &finderSig, sizeof( finderSig ), &targetAddress ) )) if(! (err = AECreateAppleEvent( eventClass, eventID, &targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &event ) )) if(! (err = AEPutKeyPtr( &event, keyDirectObject, typeObjectSpecifier, *objHandle, GetHandleSize(objHandle) ))) if(! (err = AESend( &event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil ) )) { // do something with reply }else{ // check keyErrorNumber if you care } if( event.dataHandle ) (void)AEDisposeDesc( &event ); if( reply.dataHandle ) (void)AEDisposeDesc( &reply ); if( targetAddress.dataHandle ) (void)AEDisposeDesc( &targetAddress ); } static void GetFinderDataEvent( Handle objHandle, DescType desiredType, AEDesc *result) { OSErr err = noErr; OSErr returnedErr = noErr; AppleEvent event, reply; AEDesc targetAddress; long actualSize; DescType theType; OSType finderSig = 'MACS'; event.dataHandle = reply.dataHandle = targetAddress.dataHandle = result->dataHandle = nil; result->descriptorType = typeNull; if(! (err = AECreateDesc( typeApplSignature, &finderSig, sizeof( finderSig ), &targetAddress ) )) { if(! (err = AECreateAppleEvent (kAECoreSuite, kAEGetData, &targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &event ) )) { if(! (err = AEPutKeyPtr( &event, keyDirectObject, typeObjectSpecifier, *objHandle, GetHandleSize(objHandle) ))) { if(! (err = AEPutKeyPtr( &event, keyAERequestedType, typeType, &desiredType, sizeof(desiredType) ))) { if(! (err = AESend( &event, &reply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, nil, nil ) )) { returnedErr = noErr; err = AEGetKeyPtr( &reply, keyErrorNumber, typeShortInteger, &theType, (Ptr) &returnedErr, sizeof(returnedErr), &actualSize); if (err == errAEDescNotFound || returnedErr == noErr) { if (err = AEGetParamDesc( &reply, keyAEResult, typeAEList, result )) printf("AEGetParamDesc error #%d\n", err); } else { if (returnedErr == errAENoSuchObject) printf ("No such object\n"); else printf("keyErrorNumber error #%d\n", returnedErr); } } else printf("AESend error #%d\n", err); } else printf("AEPutKeyPtr type error #%d\n", err); } else printf("AEPutKeyPtr direct object error #%d\n", err); } else printf("AECreateAppleEvent error #%d\n", err); } else printf("AECreateDesc error #%d\n", err); if( event.dataHandle ) (void)AEDisposeDesc( &event ); if( reply.dataHandle ) (void)AEDisposeDesc( &reply ); if( targetAddress.dataHandle ) (void)AEDisposeDesc( &targetAddress ); } void main( void ) { OSErr err = noErr; Handle objHandle = nil; AEDesc theList; DescType theKey, theType; long i, n, theSize; AEDesc temp; char input[1000]; // set up some SIOUX settings SIOUXSettings.asktosaveonclose = FALSE; printf("Demonstration of Jon's Commands Snag Object osax. hacked by <wkw@acmetech.com>\n\n"); printf("Two events using snagged object specifiers are sent to the Finder; the first\nin an open event which opens every folder in your top level directory, the second to close every one of the same folders.\n\n"); while( true ) { AskLine( "Trick 1 (Wayne's open & close)\n" "Trick 2 (Jon's open control panels paths)\n" "Trick 3 (Jon's hidden process creators)\n" "Trick 4 (Jon's hidden process path)\n" "(1,2,3,4): ", input ); if( input[0] == '1' ) { // send open every folder of disk 1 event objHandle = GetResource( kSnagObjType, 130 ); if( objHandle ){ HLock( objHandle ); SendFinderEvent( 'aevt', 'odoc', objHandle ); SendFinderEvent( 'core', 'clos', objHandle ); HUnlock( objHandle ); ReleaseResource( objHandle ); objHandle = nil; } } else { if (input[0] == '2') { objHandle = GetResource( kSnagObjType, 129 ); theType = typeChar; } else if (input[0] == '3') { objHandle = GetResource( kSnagObjType, 131 ); theType = typeType; } else if (input[0] == '4') { objHandle = GetResource( kSnagObjType, 132 ); theType = typeChar; } if( objHandle ){ HLock( objHandle ); GetFinderDataEvent( objHandle, theType, &theList ); HUnlock( objHandle ); ReleaseResource( objHandle ); objHandle = nil; if (theList.dataHandle != nil) { if (theList.descriptorType != typeAEList) { err = AECoerceDesc(&theList, typeAEList, &temp); if (err == noErr) { AEDisposeDesc(&theList); theList = temp; } else { printf("AECoerceDesc error #%d, descType = '%4c'\n", err, theList.descriptorType); } } // we just try to coerce things to text n = 0; err = AECountItems(&theList, &n); for (i = 1; i <= n; ++i) { err = AEGetNthPtr(&theList, i, typeChar, &theKey, &theType, &input[0], sizeof(input)-1, &theSize); if (err == noErr) { input[theSize] = 0; printf("%s\n", input); }else{ printf("AEGetNthPtr error #%d\n", err); } } printf("\n"); } } } } }